<?php

// Europa100AD
//
// OOP Classes: Bank - Dec 2009 (K. Dougans)
//
// 
//

class bank_class
{

  function __construct($bank_id = 0) {
       global $db;
       
       $this->bank_id = $bank_id;
       $this->info = $db->return_first_result("SELECT * FROM `user_business` WHERE `id`='$this->bank_id'");
   }
   

   # display html so user can do simple transactions... like deposit/withdraw
   function atm()
   {
   		$html = '';
   		
   		$accounts = '<select name="which_account">';
   		foreach ($this->list_accounts() as $key => $val)
   		{
   			$accounts .= "<option value=\"{$val['account_number']}\">{$val['account_number']}: {$val['quantity']} quadrans</option>";
   		}
   		$accounts .= '</select>';
   		
   		$html .= html_form("/bank/$this->bank_id/atm/",$accounts.'<select name="do_what"><option value="deposit">Deposit</option><option value="withdraw">Withdraw</option></select><input type="text" name="amount" value=""><input value="Confirm" type="submit">');
   		return $html;
   }
   
   function check_balance($account_number)
   {
   		global $db;
   		$account_info = $db->return_first_result("SELECT * FROM `user_business_accounts` WHERE `business_id`='$this->bank_id' AND `account_number`='$account_number'");
   		return $account_info['quantity'];
   }
   
   function list_accounts()
   {
   		global $db,$user_id;
   		$account_list = $db->fetch_real_array("SELECT * FROM `user_business_accounts` WHERE `business_id`='$this->bank_id' AND `assoc_id`='$user_id'");
   		return $account_list;
   }
   
   # Take money AWAY from user.
   function user_debit($user_id,$amount, $currency)
   {
   		global $db;
   		$sql = "UPDATE `user_finances` SET `$currency` = `$currency`-'$amount' WHERE `assoc_id` ='$user_id'";
        return $db->query($sql);
   }
   
   # Give money TO user
   function user_credit($user_id, $amount, $currency)
   {
   		global $db;
   		$sql = "UPDATE `user_finances` SET `$currency` = `$currency`+'$amount' WHERE `assoc_id` ='$user_id'";
        return $db->query($sql);
   }
   
   # Take money AWAY from the bank
   function bank_debit($bank_id, $amount, $currency, $account_number)
   {
   		global $db;
   		$sql1 = "UPDATE `user_business` SET `$currency` = `$currency`-'$amount' WHERE `id` ='$bank_id'";
   		$result1 =  $db->query($sql1);
   		$sql2 = "UPDATE `user_business_accounts` SET `quantity` = `quantity`+'$amount' WHERE `id` ='$bank_id' AND `account_number` = '$account_number'";
   		$result2 =  $db->query($sql2);
        return $db->query($sql);
   }
   
   # Give money to the bank 
   function bank_credit($bank_id, $amount, $currency)
   {
   		global $db;
   		$sql = "UPDATE `user_business` SET `$currency` = `$currency`+'$amount' WHERE `id` ='$bank_id'";
        return $db->query($sql);
   }   
   
   # Find out how much money the bank has readily available 
   function money_available()
   {
   		global $db;
   		$available_funds = $this->info['quadrans'];
        return $available_funds;
   }
   
   
   function get_link($what)
   {
   		$html = '';
   		return $html;
   }
   
   function has_account()
   {
   		return true;
   }
   
   function is_valid_account($account_number)
   {
   		global $db,$user_id;
   		$account_info = $db->return_first_result("SELECT * FROM `user_business_accounts` WHERE `business_id`='$this->bank_id' AND `account_number`='$account_number' AND `assoc_id`='$user_id'");
   		if (strlen($account_info['account_number']) > 0)
   		{
   			return true;
   		}
   		else 
   		{
   			return false;
   		}
   }
   
   function __destruct() {
     //  print "Destroying " . $this->name . "\n";
   }
   
   


} 

?>